package top.oylan.manager.controller.admin;

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.oylan.manager.entity.Banner;
import top.oylan.manager.service.BannerService;
import top.oylan.manager.tools.Directory;
import top.oylan.manager.tools.FileUploader;
import top.oylan.manager.tools.Pager;
import top.oylan.manager.tools.Result;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author abduahad
 * @Description ${description}
 * @Date 2019-04-21
 */
@RequestMapping("/admin/banner")
@Controller
public class AdminBannerController {

    @Resource
    BannerService bannerService;
    
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String index(ModelMap modelMap,
                        @RequestParam(value = "page",defaultValue = "1")Integer page){

        Pager<Banner> pager = new Pager<>();
        pager.setCurrentPage(page);
        Banner condition = new Banner();
        pager.setCondition(condition);
        pager.setList(bannerService.selectBannersByPager(pager));
        modelMap.addAttribute("pager",pager);
        return "admin/banner/list";
    }

    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String add(ModelMap modelMap){

        return "admin/banner/add";
    }

    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    public String edit(ModelMap modelMap,Integer id){
        Banner banner = bannerService.findById(id);
        modelMap.addAttribute("banner",banner);
        return "admin/banner/edit";
    }

    @RequestMapping(value = "/save",method = RequestMethod.POST)
    @ResponseBody
    public Result save(@RequestBody Banner banner){

        Result result = new Result();
        if(banner.getId()!=null && banner.getId() > 0){
            bannerService.updateById(banner,banner.getId());
        }else{
            banner.setCreateTime(new Date());
            bannerService.saveBanner(banner);
        }
        result.setCode((byte) 1);
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    public JSONObject delete(@RequestParam(value = "id",defaultValue = "0")Integer id){

        JSONObject object = new JSONObject();
        int delId = bannerService.deleteById(id);
        if(delId > 0){
            object.put("status",1);
        }else{
            object.put("status",0);
        }
        return object;
    }

    @Resource
    FileUploader fileUploader;

    @RequestMapping("/upload")
    @ResponseBody
    public JSONObject upload(MultipartFile file){
        JSONObject object = new JSONObject();
        Result res = fileUploader.upload(file, Directory.IMAGE);
        if(res.getCode() == 1){
            object.put("path",res.getData().get("path").toString());
            object.put("status",1);
        }else{
            object.put("status",0);
        }
        return object;
    }
}
